package com.wdhcr.controller;


import com.wdhcr.entity.*;
import com.wdhcr.enums.ResultCode;
import com.wdhcr.service.*;
import com.wdhcr.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;

/*
 * @description: 首页展示类
 * @author: wdhcr
 * @dateTime: 2020/3/24 15:23
 */
@RestController
public class ShowBlogController {


    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    @Autowired
    private LinkService linkService;
    @Autowired
    private UserService userService;

    /*
     * @description: 渲染左侧博客栏内容的接口
     * @params: [page, size]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/24 16:33
     */
    @CrossOrigin
    @GetMapping("showBlogs/{page}/{size}")
    public ResponseResult showBlogs(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {

        //根据更新时间倒叙排列
        Sort update_time = Sort.by(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = PageRequest.of(page, size, update_time);

        Page<Blog> pubAll = blogService.findPubAll(pageRequest);

        if (!pubAll.isEmpty()) {
            return new ResponseResult(ResultCode.SUCCESS.getCode(), "获取成功!", pubAll);
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(), "获取失败!", pubAll);
    }

    /*
     * @description: 渲染右侧博客栏内容的接口
     * @params:
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/24 16:33
     */
    @CrossOrigin
    @GetMapping("showRight")
    public ResponseResult showRight() throws Exception {
        HashMap<String, Object> result = new HashMap<>();
        try {
            List<Type> types = typeService.listTypeTop(8);
            List<Tag> tags = tagService.listTagTop(8);

            List<Blog> pubTop = blogService.findPubTop(4);


            List<Link> links = linkService.listLinkTop(4);
            User byId = userService.findById(1);
            byId.setRole(null);
            byId.setId(null);
            byId.setOpenid(null);
            result.put("types", types);
            result.put("tags", tags);
            result.put("pusTop", pubTop);
            result.put("links", links);
            result.put("admin", byId);
        } catch (Exception e) {
            throw new Exception("获取失败!");
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(), "获取成功!", result);
    }


    /*
     * @description: 根据id查询对应博客详细内容
     * @params: [id]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/24 17:23
     */
    @CrossOrigin
    @GetMapping("showBlogById/{id}")
    public ResponseResult getBlogById(@PathVariable Integer id) throws Exception {

        HashMap<String, Object> result = new HashMap<>();
        try {
            Blog byId = blogService.findById(id);
            User user = userService.findById(1);
            result.put("blog", byId);
            result.put("user", user);
            if (byId != null) {
                return new ResponseResult(ResultCode.SUCCESS.getCode(), "欢迎观看!", result);

            }
        } catch (Exception e) {
            throw new Exception("获取失败！");
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(), "获取失败!", null);
    }

    @CrossOrigin
    @PostMapping("searchShowBlog")
    public ResponseResult searchShowBlog(@RequestBody String query) {
        String dquery = null;
        try {
            dquery = URLDecoder.decode(query, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //根据更新时间倒叙排列
        Sort sort = Sort.by(Sort.Direction.DESC, "updateTime");


        List<Blog> all = blogService.findByQueryPub(sort, dquery.replace("=", ""));
        if (!all.isEmpty()) {
            return new ResponseResult(ResultCode.SUCCESS.getCode(), "获取列表成功！", all);
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(), "暂时找不到，试试其他的吧！", all);
    }
}
